/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.book.web;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.persistence.Result;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.book.entity.BkBooks;
import com.thinkgem.jeesite.modules.book.service.BooksService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 流程个人任务相关Controller
 *
 * @author ThinkGem
 * @version 2013-11-03
 */
@Controller
@RequestMapping(value = "${adminPath}/book/books")
public class BooksController extends BaseController{

    @Autowired
    private BooksService booksService;


    @ModelAttribute
    public BkBooks get(@RequestParam(required = false) String id){
        if(StringUtils.isNotBlank(id)){
            return booksService.get(id);
        }
        else{
            return new BkBooks();
        }
    }


    @RequiresPermissions("book:books:view")
    @RequestMapping(value = {"list", ""})
    public String list(BkBooks bkBooks, HttpServletRequest request, HttpServletResponse response, Model model){
        Page<BkBooks> page = booksService.findPage(new Page<BkBooks>(request, response), bkBooks);
        model.addAttribute("page", page);
        return "modules/book/books/booksList";
    }

    @RequiresPermissions("user")
    @RequestMapping("listData")
    public String listData(String booksName, Model model){
        //查询馆藏数量>0的书籍
        BkBooks books = new BkBooks();
        books.setBooksName(booksName);
        List<BkBooks> booksList = booksService.findList(books);
        model.addAttribute("booksList", booksList);
        model.addAttribute("booksName", booksName);
        return "modules/book/books/booksListData";
    }

    @RequiresPermissions("book:books:view")
    @RequestMapping("toInsert")
    public String booksInsert(String id, String booksName, String message, Model model){
        if(StringUtils.isNotEmpty(message)){
            model.addAttribute("message", message);
        }
        BkBooks bkBooks;
        if(StringUtils.isNotEmpty(id)){
            bkBooks = booksService.get(id);
            model.addAttribute("bkBooks", bkBooks);
        }
        if(StringUtils.isNotEmpty(booksName)){
            bkBooks = new BkBooks();
            bkBooks.setBooksName(booksName);
            model.addAttribute("bkBooks", bkBooks);
        }
        return "modules/book/books/booksEdit";
    }

    @RequiresPermissions("book:books:view")
    @RequestMapping("save")
    public String save(String id, String booksName, String booksNumber, Model model, RedirectAttributes redirectAttributes){
        BkBooks books = new BkBooks(id, booksName, booksNumber);
        if(StringUtils.isNotEmpty(booksName)){
            booksService.save(books);
            model.addAttribute("bkBooks", books);
            addMessage(redirectAttributes, "保存图书字段成功");
            return "redirect:" + adminPath + "/book/books/toInsert?id=" + books.getId();
        }
        else{
            model.addAttribute("booksName", booksName);
            model.addAttribute("message", "保存图书字段失败");
            return "forward:" + adminPath + "/book/books/toInsert";
        }
    }


    @RequiresPermissions("book:books:delete")
    @RequestMapping("delete")
    @ResponseBody
    public Result delete(String id){
        BkBooks books = new BkBooks();
        books.setId(id);
        booksService.delete(books);
        return new Result("删除成功");
    }
}
